/*
 * @Description: 触发消息列表配置
 * @version: 电子名片2.0
 * @Author: 尹鑫
 * @Date: 2020-12-04 11:33:08
 * @LastEditors: 尹鑫
 * @LastEditTime: 2020-12-16 10:55:46
 */
function transformParams(editList, detail, isRed) {
  editList.forEach(item => {
    if (item.transform) {
      item.transform(detail);
    } else {
      item.value = detail[item.key] || item.value;
    }
    if (isRed && !item.disabled) item.disabled = true;
  })
}
export function configData(detail) {
  const listHedar = [
    { prop: 'id', label: '触发消息编号' },
    { prop: 'notifyTitle', label: '通知标题' },
    { 
      prop: 'isSendSms', label: '发送短信', 
      show(row) {
        return row.isSendSms ? '是' : '否'
      } 
    },
    { 
      prop: 'notifyType', label: '消息类型',
      show: { '0': '无','1': '系统消息', '2': '服务通知'}
    },
    { prop: 'tagName', label: '触发条件' },
    { 
      prop: 'triggerType', label: '触发类型',
      show: {
        '1': '操作触发',
        '2': '标签触发'
      }
    },
    { 
      prop: 'status', label: '状态',
      show: { '1': '启用', '2': '停用', default: '未启用'}
    },
    { prop: 'modifierName', label: '修改人' },
    { prop: 'gmtModified', label: '修改时间' },
    { prop: 'operate', label: '操作', isTemplate: true }
  ]
  const searchs = [
    { type: 'input', key: 'keyword', value: '', placeholder: '编号，标题' },
    { 
      type: 'select', key: 'notifyType', value: '', placeholder: '消息类型', 
      selectList: [
        { value: '0', title: '无' },
        { value: '1', title: '系统消息' },
        { value: '2', title: '服务通知' }
      ]
    },
    { 
      type: 'select', key: 'triggerType', value: '', placeholder: '触发类型',
      selectList: [
        { value: '1', title: '操作触发' },
        { value: '2', title: '标签触发' }
      ]
    },
    { 
      type: 'select', key: 'status', value: '', placeholder: '状态', 
      selectList: [
        { value: '1', title: '启用' },
        { value: '2', title: '停用' }
      ]
    }
  ]
  return {
    listHedar, searchs
  }
}
export function createEditData(detail, templateList, tagList) {
  const editList = [
    { 
      type: 'select', key: 'triggerType', label: '触发类型：', value: 2, have: true, disabled: true,
      options: [
        { value: 1, title: '操作触发' },
        { value: 2, title: '标签触发' }
      ],
      change(value=this.value) {
        // const _value = value == 1;
        const _value = true;
        const _isSendSms = editList.find(item => item.key === 'isSendSms');
        _isSendSms.value = _value;
        _isSendSms.data = _value ? 
          [
            { value: true, title: '是' },
            { value: false, title: '否' }
          ] 
          :
          [
            { value: true, title: '是' },
            { value: false, title: '否' }
          ];
        editList.find(item => item.key === 'tagId').hideItem = value == 1;
        editList.find(item => item.key === 'notifyType').hideItem = false;
      }
    },
    { type: 'input', key: 'notifyTitle', label: '消息标题：', value: '', have: true },
    { 
      type: 'select', key: 'tagId', label: '触发条件：', value: '', have: true, hideItem: true,
      options: tagList
    },
    {
      type: 'radio', key: 'isSendSms', label: '发送短信：', value: false, have: true,
      data: [
        { value: false, title: '否' },
        { value: true, title: '是' }
      ],
      change(value=this.value) {
        editList.forEach(item => {
          if (item.key === 'smsId' || item.key === 'notifyTemContent' || item.key === 'smsSign') {
            item.hideItem = !value;
          }
        })
      }
    },
    {
      type: 'select', key: 'smsId', label: '短信模板：', value: '', have: true, 
      hideItem: true,
      options: templateList,
      change(value=this.value) {
        const _select = templateList.find(item => item.value === value);
        if (!_select) return;
        editList.find(item => item.key === 'notifyTemContent').value = _select.content;
      },
    },
    { type: 'textarea', key: 'notifyTemContent', label: '模板内容：', value: '', disabled: true, hideItem: true, notParams: true },
    { type: 'input', key: 'smsSign', label: '消息签名：', value: '本无名片', have: true, hideItem: true },
    {
      type: 'select', key: 'notifyType', label: '消息类型：', value: 0, have: true,
      options: [
        { value: 0, title: '无' },
        { value: 1, title: '系统消息' },
        { value: 2, title: '服务通知' }
      ],
      change(value=this.value) {
        editList.find(item => item.key === 'notifyContent').hideItem = (value == 0);
      }
    },
    { type: 'textarea', key: 'notifyContent', label: '消息内容：', value: '', hideItem: true, have: true }
  ]
  if (detail) {
    transformParams(editList, detail);
  }
  editList.forEach(item => {
    if (item.change && !item.hideItem) item.change();
  })
  return editList;
}
